﻿using OrderManage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderManageForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = new BindingList<Order>(new List<Order>());
            dataGridView1.CellClick += dgv_CellClick;
        }
        private void button1_Click(object sender, EventArgs e)//查询订单
        {
            this.dataGridView2.Rows.Clear();
            string findItem = this.查询点.Text;
            string item = this.查询内容.Text;
            List<Order> orders = new List<Order>();
            switch (findItem)
            {
                case "单号":
                    {
                        orders.Add(Program.service.findID(item));
                        dataGridView1.DataSource =orders ;
                        if (Program.service.findID(item) != null)
                        {
                            dataGridView1["总价", 0].Value = Program.service.findID(item).GetMoney();
                        }
                        break;
                    }
                case "客户":
                    {
                        orders=Program.service.findCustomer(item);
                        dataGridView1.DataSource = orders;
                        for(int i=0;i<orders.Count;i++)
                        {
                            dataGridView1["总价", i].Value = orders[i].GetMoney();
                        }
                        break;
                    }
                case "下单时间":
                    {
                        orders = Program.service.findOrderDate(item);
                        dataGridView1.DataSource = orders;
                        for (int i = 0; i < orders.Count; i++)
                        {
                            dataGridView1["总价", i].Value = orders[i].GetMoney();
                        }
                        break;
                    }
                case "总价":
                    {
                        orders = Program.service.findMoney(Double.Parse(item));
                        dataGridView1.DataSource = orders;
                        for (int i = 0; i < orders.Count; i++)
                        {
                            dataGridView1["总价", i].Value = orders[i].GetMoney();
                        }
                        break;
                    }
                case "商品":
                    {
                        orders = Program.service.findGoods(item);
                        dataGridView1.DataSource = orders;
                        for (int i = 0; i < orders.Count; i++)
                        {
                            dataGridView1["总价", i].Value = orders[i].GetMoney();
                        }
                        break;
                    }
            }
        }
        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)//点击订单显示订单明细
        {
            if (this.dataGridView1.Rows[e.RowIndex].Cells["orderIdDataGridViewTextBoxColumn"].Value==null) { return; }
            this.dataGridView2.Rows.Clear();
            Order o = Program.service.findID(this.dataGridView1.Rows[e.RowIndex].Cells["orderIdDataGridViewTextBoxColumn"].Value.ToString());
            if (o == null) { return; }
            for(int i = 0; i < o.orderDetails.count; i++) {
                int index = this.dataGridView2.Rows.Add();
                this.dataGridView2.Rows[index].Cells[0].Value = i+1+"";
                this.dataGridView2.Rows[index].Cells[1].Value = o.orderDetails.goods[i].name;
                this.dataGridView2.Rows[index].Cells[2].Value = o.orderDetails.numbers[i].ToString();
                this.dataGridView2.Rows[index].Cells[3].Value = o.orderDetails.goods[i].price.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)//删除订单
        {
            if (this.dataGridView1.SelectedRows.Count == 0) { return; }
            int a = dataGridView1.SelectedRows[0].Index;
            Program.service.deleteOrder(dataGridView1.Rows[a].Cells["orderIdDataGridViewTextBoxColumn"].Value.ToString());
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1["总价", i].Value = Program.service.GetOrders()[i].GetMoney();
            }
        }

        private void button2_Click(object sender, EventArgs e)//新建订单
        {
            Form2 form2 = new Form2("新建");
            form2.id.ReadOnly = false;
            form2.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)//修改订单
        {
            if (this.dataGridView1.SelectedRows.Count==0) { return; }
            Form2 form2 = new Form2("修改");
            form2.id.Text = this.dataGridView1.SelectedRows[0].Cells["orderIdDataGridViewTextBoxColumn"].Value.ToString();
            form2.customer.Text= this.dataGridView1.SelectedRows[0].Cells["customerDataGridViewTextBoxColumn"].Value.ToString();
            form2.date.Text = this.dataGridView1.SelectedRows[0].Cells["orderDateDataGridViewTextBoxColumn"].Value.ToString();
            form2.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string s = "";
            for(int i = 0; i < Program.service.GetOrders().Count; i++)
            {
                s += Program.service.GetOrders()[i] + "\n\n";
            }
            MessageBox.Show(s,"订单信息");
        }
    }
}
